package com.timmy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.timmy.common.RedisUtil;
import com.timmy.common.UpmsResultConstant;
import com.timmy.entity.Emp;
import com.timmy.service.DeptService;
import com.timmy.service.EmpService;
import com.timmy.util.UpmsResult;
import org.apache.commons.lang.BooleanUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.subject.Subject;


import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController {
    @Resource
    private DeptService deptService;
    @Resource
    private EmpService empService;


    private final static Logger _log = LoggerFactory.getLogger(EmpController.class);
    // 全局会话key
    private final static String UPMS_SERVER_SESSION_ID = "upms-server-session-id";
    // 全局会话key列表
    private final static String UPMS_SERVER_SESSION_IDS = "upms-server-session-ids";
    // code key
    private final static String UPMS_SERVER_CODE = "upms-server-code";

    /**
     * 登录
     *
     * @param model
     * @param request
     * @return
     */

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object login(Model model, HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String rememberMe = request.getParameter("rememberMe");

        if (StringUtils.isBlank(username)) {
            return new UpmsResult(UpmsResultConstant.EMPTY_USERNAME, "账号不能为空");
        }
        if (StringUtils.isBlank(password)) {
            return new UpmsResult(UpmsResultConstant.EMPTY_PASSWORD, "密码不能为空");
        }

        return null;

    }

    /**
     * 查询所有的员工
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/listAllEmployee", method = RequestMethod.GET)
    public String listAllEmployee(Model model, HttpSession session, HttpServletRequest request, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        //引入pageHelper分页插件
        //在查询之前只需要传入页码，以及每页的大小
        PageHelper.startPage(pn, 5);
//        startPage后面紧跟的查询就是分页查询
        List<Emp> empList = empService.listAllEmployee();

        //采用pageInfo对查询结果进行包装,只需要将pageInfo交给页面，即可获取当前页，是否有下一页等详细信息
//        new PageInfo中第二个参数表示需要连续显示的页数
        PageInfo page = new PageInfo(empList, 5);

        model.addAttribute("empList", empList);
        model.addAttribute("page", page);
        return "list";
    }

}
